package com.hxc.pay.wx.config;


import com.hxc.pay.CommonPayTemplate;
import com.hxc.pay.JsapiPayTemplate;
import com.hxc.pay.NativePayTemplate;
import com.hxc.pay.wx.core.WxCommonPayTemplate;
import com.hxc.pay.wx.core.WxJsapiPayTemplate;
import com.hxc.pay.wx.core.WxNativePayTemplate;
import com.hxc.pay.wx.utils.SignUtils;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.io.File;
import java.io.FileInputStream;
import java.security.PrivateKey;

@Configuration
@EnableConfigurationProperties(WxPayProperties.class)
@Import(WebInit.class)
public class WxPayAutoConfiguration {


    @Bean
    public CloseableHttpClient httpClient(WxPayProperties wxPayProperties){
        // 加载商户私钥（privateKey：私钥字符串）
//        PrivateKey merchantPrivateKey = PemUtil
//                .loadPrivateKey(new ByteArrayInputStream(privateKey.getBytes("utf-8")));
        CloseableHttpClient httpClient = null;
        try {
            PrivateKey merchantPrivateKey=
            PemUtil.loadPrivateKey(new FileInputStream(new File(wxPayProperties.getPrivateKeyFilePath())));

            // 加载平台证书（mchId：商户号,mchSerialNo：商户证书序列号,apiV3Key：V3密钥）
            AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
                    new WechatPay2Credentials(wxPayProperties.getMchId(), new PrivateKeySigner(wxPayProperties.getMchSerialNo(), merchantPrivateKey)),wxPayProperties.getApiV3Key().getBytes("utf-8"));

            // 初始化httpClient
            httpClient = WechatPayHttpClientBuilder.create()
                    .withMerchant(wxPayProperties.getMchId(), wxPayProperties.getMchSerialNo(), merchantPrivateKey)
                    .withValidator(new WechatPay2Validator(verifier)).build();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("------1.检查网络是否正常------- \r/n------2.请检查application.yml中的私钥文件路径配置，以及路径下是否有对应的私钥文件------");
        }

        return httpClient;
    }



    @Bean
    public CommonPayTemplate commonPayTemplate(WxPayProperties wxPayProperties,CloseableHttpClient httpClient){
        return new WxCommonPayTemplate(wxPayProperties,httpClient);
    }

    @Bean
    public JsapiPayTemplate jsapiPayTemplate(WxPayProperties wxPayProperties,CloseableHttpClient httpClient){
        return new WxJsapiPayTemplate(wxPayProperties,httpClient);
    }

    @Bean
    public NativePayTemplate nativePayTemplate(WxPayProperties wxPayProperties,CloseableHttpClient httpClient){
        return new WxNativePayTemplate(wxPayProperties, httpClient);
    }

    @Bean
    public SignUtils signUtils(WxPayProperties wxPayProperties){
        return new SignUtils(wxPayProperties);
    }
}
